package com.qf.flash.ui.circle;

import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flashlib.base.LazyBaseFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/2/22 on 20:27
 * 描述:
 * 作者:戴旭东
 */
public class CircleFragment extends LazyBaseFragment {

    @BindView(R.id.mTabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.mViewPager)
    ViewPager mViewPager;
    List<Fragment> fragments = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    List<String> ss = new ArrayList<>();
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.ll_to_hot)
    LinearLayout llToHot;
    @BindView(R.id.ll_to_ability)
    LinearLayout llToAbility;

    @Override
    protected int setContentView() {
        return R.layout.fragment_circle;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText("圈子");
        tvTopBack.setVisibility(View.INVISIBLE);
        fragments.add(RecommendFragment.newInstance());//推荐
        fragments.add(StrategyFragment.newInstance());  //攻略
        fragments.add(VideoFragment.newInstance()); // 视频
        fragments.add(FunnyFragment.newInstance("0"));//搞笑
        fragments.add(FunnyFragment.newInstance("1"));

        titles.add("推荐");
        titles.add("攻略");
        titles.add("视频");
        titles.add("搞笑");
        titles.add("生活");
        mTabLayout.addTab(mTabLayout.newTab().setText("推荐"));
        mTabLayout.addTab(mTabLayout.newTab().setText("攻略"));
        mTabLayout.addTab(mTabLayout.newTab().setText("视频"));
        mTabLayout.addTab(mTabLayout.newTab().setText("搞笑"));
        mTabLayout.addTab(mTabLayout.newTab().setText("生活"));
        //tab选中变大
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.title_text_layout, null);
                textView.setTextSize(18);
                textView.setText(tab.getText());
                tab.setCustomView(textView);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        // tab下划线宽度
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 12, 12);
            }
        });

        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return titles.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });

        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(5);

    }


    @Override
    protected void lazyLoad() {

    }


    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
//通过反射获取到
        Class tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
//设置模式
        tabStrip.setAccessible(true);
        //获得tabview
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
//设置tabView的padding为0，并且设置了margin
        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }


    @OnClick({R.id.ll_to_hot, R.id.ll_to_ability})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_to_hot://r 热门榜
                HotListActivity.start(getActivity());
                break;
            case R.id.ll_to_ability:// 能力榜
                AbilityListActivity.start(getActivity());
                break;
        }
    }
}
